package net.mcg_server.jbi.xml;

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.mcg_server.jbi.Program;
import net.mcg_server.jbi.data.Game;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XMLReader {


	private File xmlFile;
	private DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	private DocumentBuilder dbuilder;

	public void readXml(final Program program, String loc){
		try{
			program.clearGameList();
			dbuilder = dbFactory.newDocumentBuilder();

			xmlFile = new File(loc);

			Document doc = dbuilder.parse(xmlFile);

			doc.getDocumentElement().normalize();
			NodeList nodeList = doc.getElementsByTagName("game");

			for(int i = 0; i < nodeList.getLength(); i++){
				program.getGameList().add(getGame(nodeList.item(i)));
			}
		}catch(SAXException|IOException|ParserConfigurationException e){
			e.printStackTrace();
		}
		
		program.getMainPanel().loadData();
	}

	public void readXml(final Program program){


		try{

			dbuilder = dbFactory.newDocumentBuilder();

			JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home")+"/Documents/JBI/GameLists");
			fileChooser.setMultiSelectionEnabled(false);

			FileFilter filter = new FileNameExtensionFilter("XML File", "xml");
			fileChooser.setFileFilter(filter);

			int value = fileChooser.showOpenDialog(null);

			if(value == JFileChooser.APPROVE_OPTION){

				program.clearGameList();

				try{
					xmlFile = fileChooser.getSelectedFile();
				}catch(Exception e){
					System.out.println("Error location 1");
					e.printStackTrace();
				}

				Document doc = dbuilder.parse(xmlFile);

				doc.getDocumentElement().normalize();
				NodeList nodeList = doc.getElementsByTagName("game");

				for(int i = 0; i < nodeList.getLength(); i++){
					program.getGameList().add(getGame(nodeList.item(i)));
				}
			}

		}catch(SAXException | ParserConfigurationException | IOException e){
			System.out.println("Error location 2");
			e.printStackTrace();
		}
		program.getMainPanel().loadData();

	}

	private Game getGame(Node node) {
		Game game = new Game();

		if(node.getNodeType() == Node.ELEMENT_NODE){
			Element element = (Element) node;
			game.setTitle(getTagValue("title", element));
			game.setDescription(getTagValue("description", element));
			if(getTagValue("isBeaten", element).equals("true")){
				game.setBeaten(true);
			}else{
				game.setBeaten(false);
			}
		}

		return game;
	}

	private static String getTagValue(String string, Element element) {
		NodeList nodeList = element.getElementsByTagName(string).item(0).getChildNodes();
		Node node = (Node) nodeList.item(0);
		return node.getNodeValue();
	}
}
